import {Component, ViewChild} from '@angular/core';
import {IonicPage, LoadingController, NavController} from 'ionic-angular';
import {HttpClient} from "@angular/common/http";
import {HttpServicesProvider} from "../../providers/http-services/http-services";
import {ConfigProvider} from "../../providers/config/config";
import {UrlProvider} from "../../providers/url/url";
import {StorageProvider} from "../../providers/storage/storage";
import {ValidateProvider} from "../../providers/validate/validate";
import {UtilsProvider} from "../../providers/utils/utils";
import {WechatProvider} from "../../providers/wechat/wechat";

@IonicPage({
    segment: 'index'
})
@Component({
    selector: 'page-home',
    templateUrl: 'home.html'
})
export class HomePage {
    @ViewChild('layer') layer;
    banners = [];
    operates = [];
    notices = [];
    commends = [];
    msg = {};

    constructor(
        public navCtrl: NavController,
        public http: HttpClient,
        public loadingCtrl: LoadingController,
        public config: ConfigProvider,
        public httpService: HttpServicesProvider,
        public url: UrlProvider,
        public storage: StorageProvider,
        public validate: ValidateProvider,
        public util: UtilsProvider,
        public wechat: WechatProvider
    ) {
       
    }

    ionViewCanEnter(){        
        return this.validate.checkLogin();
        
    }

    ionViewWillEnter(){
        this.getData();       
    }
    ionViewDidLoad() {
    }

    getData() {
        //读取本地数据
        
        let homeData = this.storage.get('homeData');
        if(homeData){
            homeData = JSON.parse(homeData);
            //如果数据已缓存,并且未过期，那么直接读取本地数据
            if(homeData['expire_time'] > this.util.getTime()){
                this.banners = homeData['banners'];
                this.operates = homeData['operate_list']['lists'];
                this.notices = homeData['texts'];
                this.commends = homeData['images'];
                return false;
            }
        }

        this.httpService.requestData('/Home/index', response => {
            if(response['error'] === 200){
                //全部数据
                let data = response['data'];
                //banner列表
                this.banners = data['banners'];
                // 操作指南
                this.operates = data['operate_list']['lists'];
                //滚动文本信息
                this.notices = data['texts'];
                //推荐广告位数据
                this.commends = data['images'];
                data['expire_time'] = this.util.getTime()+3600;
                this.storage.set('homeData',JSON.stringify(data));
            }else{
                this.util.presentAlert('系统繁忙，请稍后再试');
            }
        });
        
    }
    /**
     * 跳转需要判断登录的页面使用的方法
     * */
    pageRedirect(page, params={}) {
        this.navCtrl.push(page, params).then(success => {
            if (success !== true) {//页面需要登录后跳转
                this.navCtrl.push('LoginPage', params);
            }
        })
    }
    /**
     * 页面跳转
     * */
    goPage(page,params = {}){
        if(page=='javascript:void(0);'){
            return false;
        }
        this.navCtrl.push(page,params);
    }
    goNull(){
        this.msg = {
            title: '程序员开发中',
            content: '<img src="assets/imgs/coding.jpg">'
        };
        this.layer.open = true;
        return false;
    }

    /**
     * 跳转到门店库
     * */
    goStores(){
        let apiUrl = "/user_info/check_level";
        this.httpService.requestPostData(apiUrl,{
            token: this.config.getToken()
        }, (response)=>{
            if(response['error'] == 200 && response['reg_step'] == 2){
                this.navCtrl.push('StoresPage');
            }else{
                this.util.presentConfirm('您无权使用此功能，请完善注册资料；现在去完善？', ()=>{
                    this.navCtrl.push('RegisterPage');
                });
            }
        });
    }

    /**
     * 跳转到人才库
     * */
    goTalentPool(){
        let apiUrl = "/user_info/check_level";
        this.httpService.requestPostData(apiUrl,{
            token: this.config.getToken()
        }, (response)=>{
            if(response['error'] == 200 && response['reg_step'] == 2){
                this.navCtrl.push('TalentPoolSearchPage');
            }else{
                this.util.presentConfirm('您无权使用此功能，请完善注册资料；现在去完善？', ()=>{
                    this.navCtrl.push('RegisterPage');
                });
            }
        });
    }
}
